package util;

import gui.panel.CenterPanel;

import javax.swing.*;
import java.awt.*;
import java.io.File;

public class GUIUtil {
    private static String imageFolder = "E:/college/hutubill/img";

    //校验一个输入框内容是否是空
    public static boolean checkEmpty(JTextField tf,String input){
        String text = tf.getText().trim();
        if(0==text.length()){
            JOptionPane.showMessageDialog(null,input+"不能为空");
            tf.grabFocus();
            return false;
        }
        return true;
    }

    //检验一个组件内容是否是数字格式
    public static boolean checkNumber(JTextField tf,String input){
        if(!checkEmpty(tf,input)){
            return false;
        }
        String text = tf.getText().trim();
        try{
            Integer.parseInt(text);
            return true;
        }catch (NumberFormatException e){
            JOptionPane.showMessageDialog(null,input+"需要是整数");
            tf.grabFocus();
            return false;
        }
    }

    //校验一个组件的内容是否是零
    public static boolean checckZero(JTextField tf,String input){
        if(!checkNumber(tf,input)){
            return false;
        }
        String text = tf.getText().trim();
        if(0==Integer.parseInt(text)){
            JOptionPane.showMessageDialog(null,input+"不能为零");
            tf.grabFocus();
            return false;
        }
        return true;
    }

    //给多个组件设置前景色
    public static void setColor(Color color,JComponent... cs){//可变数量的参数
        for(JComponent c:cs){
            c.setForeground(color);
        }
    }

    //给按钮设置图标和文本以及提示文字
    public static void setImageIcon(JButton b,String filename,String tip){
        ImageIcon i = new ImageIcon(new File(imageFolder,filename).getAbsolutePath());
        b.setIcon(i);
        b.setPreferredSize(new Dimension(61,81));
        b.setToolTipText(tip);
        b.setVerticalTextPosition(JButton.BOTTOM);
        b.setHorizontalTextPosition(JButton.CENTER);
        b.setText(tip);
    }

    //使用水晶皮肤
    public static void useLNF(){
        try {
            UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
    }

    public static void useSystem(){
        try{
            UIManager.getSystemLookAndFeelClassName();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
    /** 
    * @Description: 显示界面
    * @Param: p
    * @Param: strechRate 拉伸比例1表示满屏幕
    * @return: void 
    * @Author: desperado
    * @Date: 2018/7/25 
    */ 
    public static void showPanel(JPanel p,double strechRate){
        GUIUtil.useLNF();
        JFrame j = new JFrame();
        j.setSize(500,500);
        j.setLocationRelativeTo(null);
        CenterPanel cp = new CenterPanel(strechRate);
        j.setContentPane(cp);
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setVisible(true);
        cp.show(p);
    }

    public static void showPanel(JPanel p){
        showPanel(p,0.85);
    }

    public static int getInt(JTextField tf) {
        return Integer.parseInt(tf.getText());
    }
}
